package com.web.admin.web.cms.pan;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.config.ErrorConfig;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.UploadFile;
import com.blue.http.message.View;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.UpdateModel;
import com.web.base.join.UserJoinHandler;
import com.web.base.user.facade.UserFacade;
import com.web.admin.global.model.User;
import com.web.admin.global.util.HttpContext;
import com.web.cms.pan.exception.FolderException;
import com.web.cms.pan.facade.FileDataFacade;
import com.web.cms.pan.message.FileDataRequest;
import com.web.cms.pan.message.FileDataResponse;
import com.web.cms.pan.message.FolderFileRequest;
import com.web.core.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @author zhengjin
 * @since 1.0 2018年04月17日
 */
@Controller("panFileDataController")
@Http(url = "/cms/pan")
public class FileDataController
{
	private static Logger logger = LoggerFactory.getLogger(FileDataController.class);
	
	@Autowired
	private FileDataFacade fileDataFacade;
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private StorageClient storageClient;
	@Resource
	private HttpContext httpContext;

	private DictParser dictParser = DictParser.getInstance();
	private ErrorConfig errorConfig = ErrorConfig.getInstance();

	public FileDataController()
	{
	}

	@Http(url = "/file_data_upload", method = HttpMethod.POST)
	public void upload(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		UploadFile file = request.getFile();
		int folderId = request.getPostInt("folderId", 0);
		if (file.getFile() == null)
			throw errorConfig.getException(ErrorConfig.ERROR_UPLOAD);

		try
		{
			String key = storageClient.save(Bucket.PAN, file.getInputStream(), file.getOriginalName(), null);
			this.addUpload(file, key, folderId, user.getId());
		}
		catch (Exception e)
		{
			logger.error(errorConfig.get(ErrorConfig.ERROR_UPLOAD) + "：", e);
			throw errorConfig.getException(ErrorConfig.ERROR_UPLOAD);
		}
	}

	private void addUpload(UploadFile file, String key, Integer folderId, Integer userId)
	{
		FileDataRequest request = new FileDataRequest();
		request.setUserId(userId);
		request.setFolderId(folderId);
		request.setName(file.getOriginalName());
		request.setKey(key);
		request.setMimeType(file.getContentType());
		request.setSize(file.getSize());
		fileDataFacade.add(request);
	}

	@Http(url = "/file_data_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		FolderFileRequest folderFileRequest = request.getPostObject(FolderFileRequest.class);
		folderFileRequest.setUserId(user.getId());
		ValidationUtil.valid(folderFileRequest, UpdateModel.class);
		fileDataFacade.update(folderFileRequest);
	}

	@Http(url = "/file_data_state", method = HttpMethod.POST)
	public void state(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		FolderFileRequest folderFileRequest = request.getPostObject(FolderFileRequest.class);
		folderFileRequest.setUserId(user.getId());
		fileDataFacade.state(folderFileRequest);
	}

	@Http(url = "/file_data_operate", method = HttpMethod.POST)
	public void operate(HttpRequest request)
	{
		int folderFileId = request.getPostInt("srcId", 0);
		int folderId = request.getPostInt("destId", 0);
		int type = request.getPostInt("type", 0);
		if (type == 0)
			throw new FolderException("未知操作");

		User user = httpContext.getUser(request);
		fileDataFacade.operate(user.getId(), folderFileId, folderId, type);
	}

	@Http(url = "/file_data_delete", method = HttpMethod.POST)
	public void delete(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		String ids = request.getPost("ids");
		fileDataFacade.delete(user.getId(), ids);
	}

	@Http(url = "/file_data_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		FileDataRequest fileDataRequest = request.getQueryObject(FileDataRequest.class);
		Page<FileDataResponse> page = fileDataFacade.listFilePage(fileDataRequest, Const.PAGE_START, Const.PAGE_SIZE);
		UserJoinHandler.userJoin(page.getObjectList(), userFacade);
		return View.createBuilder().setView("/cms/pan/file_data_list")
						.put("model", fileDataRequest)
						.put("map", dictParser.getStringMap())
						.put("page", page)
						.build();
	}

	@Http(url = "/file_data_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		FileDataRequest fileDataRequest = request.getPostObject(FileDataRequest.class);
		int start = request.getPostInt("start", 1);
		Page<FileDataResponse> page = fileDataFacade.listFilePage(fileDataRequest, start, Const.PAGE_SIZE);
		UserJoinHandler.userJoin(page.getObjectList(), userFacade);
		return View.createBuilder().setView("/cms/pan/file_data_list_grid")
						.put("page", page)
						.build();
	}

}
